<?php

namespace App\Http\Controllers\AdminApi;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Exception;

class RegionController extends BaseController
{
    public function initialize(Request $request)
    {
        parent::initialize($request);

        $this->modelName = 'Region';

        $this->defaultOrder = [
            ['filed' => 'id', 'order' => 'ASC'],
        ];
    }

    /**
     * Tree
     */
    public function tree(Request $request)
    {
        $param = $request->all();

        $page = 1;
        $limit = 20;
        if (!empty($param['page'])) {
            $page = (int)$param['page'];
        }
        if (!empty($param['limit'])) {
            $limit = (int)$param['limit'];
        }
        $sort_field = $param['sort_field'] ?? '';
        $sort_value = $param['sort_value'] ?? '';

        $className = '\\App\\Models\\' . $this->modelName;
        $model = new $className;
        $field = $this->defaultField;
        $orders = $this->defaultOrder;
        $where = $this->getFilterWhere($request);
        if ($this->defaultWhere) {
            $where = array_merge($this->defaultWhere, $where);
        }
        if ($sort_field && $sort_value) {
            $order[] = [
                'filed' => $sort_field,
                'order' => $sort_field
            ];
        }

        $query = $model->query();
        foreach ($where as $value) {
            $query->where($value[0], $value[1], $value[2]);
        }
        foreach ($orders as $order) {
            $query->orderBy($order['filed'], $order['order']);
        }
        $list = $query->take($limit)->skip($page)->get()->toArray();
        if ($list) {
            // 处理列表数据
            $list = list_to_tree($list, 'area_code', 'parent_code', 'children', 0);
        }
        $count = $query->count();
        $data = [
            'data' => $list,
        ];

        return api_response(self::STATUS_SUCCESS, '', $data);
    }

    /**
     * 处理列表数据
     */
    protected function handleListData($data)
    {
        $className = '\\App\\Models\\' . $this->modelName;
        $model = new $className;

        //$data = $model->formatDatas($data);
        foreach ($data as &$item) {
            if ($this->sourceUi == self::SOURCE_UI_2) {
                if ($item['level'] < 3) {
                    $item['isLeaf'] = false;
                } else {
                    $item['isLeaf'] = true;
                }
            } else {
                if ($item['level'] < 3) {
                    $item['hasChildren'] = true;
                    $item['children'] = [];
                }
            }
        }

        return $data;
    }

    /**
     * 筛选条件
     */
    protected function getFilterWhere($request)
    {
        $param = $request->all();
        $where = [];
        if (isset($param['parent_code']) && $param['parent_code']) {
            $where[] = ['parent_code', '=', $param['parent_code']];
        } else {
            $where[] = ['level', '=', 0];
        }

        return $where;
    }
}
